package com.example.link.testfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.link.testfragment.fragment.PlusFourFragment;
import com.example.link.testfragment.fragment.PlusOneFragment;
import com.example.link.testfragment.fragment.PlusOneFragmentNew;
import com.example.link.testfragment.fragment.PlusThreeFragment;
import com.example.link.testfragment.fragment.PlusTwoFragment;
import com.example.link.testfragment.view.SlideMenu;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends AppCompatActivity {
    @BindView(R.id.ibMenu)
    ImageButton ibMenu;
    @BindView(R.id.viewPage)
    ViewPager viewPage;
    @BindView(R.id.rb1)
    RadioButton rb1;
    @BindView(R.id.rb2)
    RadioButton rb2;
    @BindView(R.id.rb3)
    RadioButton rb3;
    @BindView(R.id.rb4)
    RadioButton rb4;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.tv_user)
    TextView tvUser;
    @BindView(R.id.tv_family)
    TextView tvFamily;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.slideMenu)
    SlideMenu slideMenu;
    private List<Fragment> fragments;
    private PagerAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        // 给viewPager设置适配器
        setPagerAdapter();
        // 设置监听 实现联动
        setListener();
    }

    // 设置监听 实现联动
    private void setListener() {
        // radioGroup控制viewPager
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb1:
                        viewPage.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        viewPage.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        viewPage.setCurrentItem(2);
                        break;
                    case R.id.rb4:
                        viewPage.setCurrentItem(3);
                        break;
                }
            }
        });

        // viewPager控制radiogroup
        viewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        rb1.setChecked(true);
                        break;
                    case 1:
                        rb2.setChecked(true);
                        break;
                    case 2:
                        rb3.setChecked(true);
                        break;
                    case 3:
                        rb4.setChecked(true);
                        break;
                }
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            public void onPageScrollStateChanged(int arg0) {

            }
        });
        ibMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                slideMenu.switchMenu();
            }
        });
    }

    // 给viewPager设置适配器
    private void setPagerAdapter() {
        fragments = new ArrayList<Fragment>();
        // TODO 给fragments集合添加fragment
        fragments.add(new PlusOneFragment());
        fragments.add(new PlusTwoFragment());
        fragments.add(new PlusThreeFragment());
        fragments.add(new PlusFourFragment());
        pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        viewPage.setAdapter(pagerAdapter);
    }

    /**
     * 声明viewPager的适配器
     */
    class PagerAdapter extends FragmentPagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int index) {
            return fragments.get(index);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

}
